<?php
class Mageex_AdvanceSeo_Model_Mysql4_Core_Url_Rewrite_Collection extends Mage_Core_Model_Mysql4_Url_Rewrite_Collection
{

    protected function _initSelect()
    {
        $this->getSelect()->from(array('main_table' => $this->getResource()->getMainTable()), array('*', new Zend_Db_Expr('LENGTH(request_path)')));
        return $this;
    }

    public function sortByLength($spec = 'DESC'){
        $this->getSelect()->order(new Zend_Db_Expr('LENGTH(request_path) ' . $spec));
        return $this;
    }

    public function filterAllByProductId($productId, $useCategories = false)
    {
        if ($useCategories == 1) {
            $this->getSelect()->where('product_id = ? AND category_id is not null',  $productId, Zend_Db::INT_TYPE);
        } else if ($useCategories == 2) {
            $this->getSelect()->where('product_id = ? AND category_id is null', $productId, Zend_Db::INT_TYPE);
        } else {
            $this->getSelect()->where('product_id = ?', $productId, Zend_Db::INT_TYPE);
        }
        return $this;
    }

    public function filterByIdPath($idPath)
    {
        $this->getSelect()
            ->where('id_path = ?', $idPath);
        return $this;
    }
}